<template>
  <div class="login">
    <div class="login-left">
      <div class="login-left-logo">
        <img :src="siteLogo" />
      </div>
      <div class="login-left-img">
        <img :src="loginMain" />
      </div>
      <img :src="loginBg" class="login-left-waves" :style="[loginBgRight?{right: loginBgRight}:'']"/>
    </div>
    <div class="login-right flex z-10">
      <div class="login-right-warp flex-margin">
        <h3 class="login-right-warp-main-title">知本科学欢迎您！</h3>
        <div class="login-right-warp-main-form">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="账号"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="user"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="new-password"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="password"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaOnOff">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="validCode"
                  class="el-input__icon input-icon"
                />
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <el-checkbox
              v-model="loginForm.rememberMe"
              style="margin: 0px 0px 25px 0px"
              class="remember-checkbox"
              >记住密码</el-checkbox
            >
            <el-form-item style="width: 100%">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="width: 100%"
                @click.native.prevent="handleLogin"
                class="login-btn"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right" v-if="register">
                <router-link class="link-type" :to="'/register'"
                  >立即注册</router-link
                >
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2022 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import loginMain from "@/assets/login-main.svg";
import loginBg from "@/assets/login-bg.svg";
import siteLogo from "@/assets/logo-site.png";
export default {
  name: "Login",
  data() {
    return {
      loginBgRight:"",
      codeUrl: "",
      loginMain,
      loginBg,
      siteLogo,
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: false,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    //this.getCode();
     this.$nextTick(() => {
      this.resizeFun();
      // this.resizeFun窗口变化执行的方法
      window.addEventListener("resize", this.resizeFun);
    });
 

    this.getCookie();
  },
  
   beforeDestroy() {
    //离开页面时删除该监听
   window.removeEventListener("resize", this.resizeFun);
  },
  methods: {
    resizeFun() {
      let  clientHeight = Number(document.documentElement.clientHeight)
      this.loginBgRight = -Math.floor(100*clientHeight/969)+"px"
      // 窗口变化执行的操作
         console.log(document.documentElement.clientHeight,"????")
    },
 
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch((e) => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>
<style rel="stylesheet/scss" lang="scss">
.remember-checkbox {
  .el-checkbox__input.is-checked + .el-checkbox__label {
    color: #167dd7;
  }
  .el-checkbox__input.is-checked .el-checkbox__inner,
  .el-checkbox__input.is-indeterminate .el-checkbox__inner {
    background-color: #167dd7;
    border-color: #167dd7;
  }
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
@media screen and (max-width: 1200px) and (min-width: 576px) {
  .login {
    .login-left {
      .login-left-img {
        top: 90% !important;
        left: 12% !important;
        width: 30% !important;
        height: 18% !important;
      }
    }
    .login-right {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
}

.login-btn {
  background-color: #167dd7;
  border-color: #167dd7;
}

.login {
  display: flex;

  height: 100%;
  .login-left {
    flex: 1;
    position: relative;
    background-color: rgb(211, 239, 255);
    margin-right: 100px;

    .login-left-logo {
      display: flex;
      align-items: center;
      position: absolute;
      top: 50px;
      left: 80px;
      z-index: 1;
      img {
        height: 30px;
      }
    }

    .login-left-img {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 100%;
      height: 52%;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .login-left-waves {
      display: block;
      position: absolute;
      top: 0;
      right: -94px;
      height: 100%;
    }
  }
  .login-right {
    display: flex;
    width: 50%;
    .login-right-warp {
      display: flex;
      flex-direction: column;
      align-items: center;
      border: 1px solid #167dd7;
      border-radius: 3px;
      width: 500px;
      height: 500px;
      position: relative;
      overflow: hidden;
      background-color: #fff;
      margin: auto;
      .login-right-warp-main-form {
        flex: 1;
        padding: 0 50px 50px;
      }
    }
  }
}
.login-right-warp-main-title {
  height: 130px;
  line-height: 130px;
  font-size: 27px;
  text-align: center;
  letter-spacing: 3px;

  animation-delay: 0.3s;
  color: #303133;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
